﻿namespace Yom.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// 公告数据操作
    /// 
    /// 创建: 邱柯皓 2009/02/04 11:16
    /// </summary>
    public class Note
    {
        #region 私有成员定义
        private readonly DBHelper db = new DBHelper();
        #endregion

        #region 构造函数
        public Note() { }
        #endregion

        #region 私有方法
        /// <summary>
        /// 通过DataReader获取公告实体对象
        /// 
        /// 创建: 邱柯皓 2009/02/04 11:18
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回公告实体对象</returns>
        private Yom.Model.Note GetModel(SqlDataReader dr)
        {
            Yom.Model.Note model = new Yom.Model.Note();
            model.AdminID = db.GetInt(dr["AdminID"]);
            model.Content = db.GetString(dr["Content"]);
            model.IsTop = db.GetBool(dr["IsTop"]);
            model.NoteID = db.GetInt(dr["NoteID"]);
            model.PostIP = db.GetString(dr["PostIP"]);
            model.PostTime = db.GetDateTime(dr["PostTime"]);
            model.Title = db.GetString(dr["Title"]);
            model.Url = db.GetString(dr["Url"]);

            return model;
        }

        /// <summary>
        /// 通过DataReader获取公告列表
        /// 
        /// 创建: 邱柯皓 2009/02/04 11:20
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回公告列表</returns>
        private List<Yom.Model.Note> GetList(SqlDataReader dr)
        {
            List<Yom.Model.Note> list = new List<Yom.Model.Note>();

            while (dr.Read())
                list.Add(GetModel(dr));

            return list;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加公告
        /// 
        /// 创建: 邱柯皓 2009/02/04 11:21
        /// </summary>
        /// <param name="model">公告实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Add(Yom.Model.Note model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("INSERT INTO [Yom_Note] (");
            strSQL.Append("[AdminID], [Title], [Content], [Url], [IsTop], [PostTime], [PostIP]");
            strSQL.Append(") VALUES (");
            strSQL.Append("@AdminID, @Title, @Content, @Url, @IsTop, @PostTime, @PostIP)");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@AdminID", DbType.Int32, model.AdminID),
                db.CreateInSqlParameter("@Title", DbType.String, model.Title),
                db.CreateInSqlParameter("@Content", DbType.String, model.Content),
                db.CreateInSqlParameter("@Url", DbType.String, model.Url),
                db.CreateInSqlParameter("@IsTop", DbType.Boolean, model.IsTop),
                db.CreateInSqlParameter("@PostTime", DbType.DateTime, model.PostTime),
                db.CreateInSqlParameter("@PostIP", DbType.String, model.PostIP)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 修改公告
        /// 
        /// 创建: 邱柯皓 2009/02/04 13:20
        /// </summary>
        /// <param name="model">公告实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Update(Yom.Model.Note model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE [Yom_Note] SET ");
            strSQL.Append("[AdminID]=@AdminID, [Title]=@Title, [Content]=@Content, [Url]=@Url, ");
            strSQL.Append("[IsTop]=@IsTop, [PostTime]=@PostTime, [PostIP]=@PostIP ");
            strSQL.Append("WHERE [NoteID]=@NoteID");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@AdminID", DbType.Int32, model.AdminID),
                db.CreateInSqlParameter("@Title", DbType.String, model.Title),
                db.CreateInSqlParameter("@Content", DbType.String, model.Content),
                db.CreateInSqlParameter("@Url", DbType.String, model.Url),
                db.CreateInSqlParameter("@IsTop", DbType.Boolean, model.IsTop),
                db.CreateInSqlParameter("@PostTime", DbType.DateTime, model.PostTime),
                db.CreateInSqlParameter("@PostIP", DbType.String, model.PostIP),
                db.CreateInSqlParameter("@NoteID", DbType.Int32, model.NoteID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 删除公告
        /// 
        /// 创建: 邱柯皓 2009/02/04 13:23
        /// </summary>
        /// <param name="NoteID">公告编号</param>
        /// <returns>返回成功标识</returns>
        public bool Delete(int NoteID)
        {
            string strSQL = "DELETE FROM [Yom_Note] WHERE [NoteID]=@NoteID";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@NoteID", DbType.Int32, NoteID) };

            return db.ExecuteNonQuery(CommandType.Text, strSQL, cmdParms) > 0;
        }

        /// <summary>
        /// 获取公告实体对象
        /// 
        /// 创建: 邱柯皓 2009/02/04 13:24
        /// </summary>
        /// <param name="NoteID">公告编号</param>
        /// <returns>返回公告实体对象</returns>
        public Yom.Model.Note GetModel(int NoteID)
        {
            string strSQL = "SELECT * FROM [Yom_Note] WHERE [NoteID]=@NoteID";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@NoteID", DbType.Int32, NoteID) };

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, cmdParms))
            {
                Yom.Model.Note model = new Yom.Model.Note();

                while(dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 分页获取公告列表
        /// 
        /// 创建: 邱柯皓 2009/02/04 13:27
        /// </summary>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="condition">查询条件</param>
        /// <param name="fldName">排序字段</param>
        /// <param name="sort">排序方式, true为正序, false为倒序</param>
        /// <returns>返回公告列表</returns>
        public List<Yom.Model.Note> GetList(int pageSize, int pageIndex, string condition, string fldName, bool sort)
        {
            using (SqlDataReader dr = db.GetPager(pageSize, pageIndex, "[Yom_Note]", fldName, condition, sort))
            {
                List<Yom.Model.Note> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 获取公告数量
        /// 
        /// 创建: 邱柯皓 2009/03/07 16:26
        /// </summary>
        /// <returns>返回公告数量</returns>
        public int GetCount()
        {
            string strSQL = "SELECT COUNT(*) FROM [Yom_Note]";
            object m = db.ExecuteScalar(CommandType.Text, strSQL, null);

            return db.GetInt(m);
        }
        #endregion
    }
}